//
//  KewaikuozhanCell.m
//  Learn
//
//  Created by chijianguo on 2024/3/29.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "KewaikuozhanCell.h"
#import "ButtonCollectionCell.h"

@interface KewaikuozhanCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic , strong)NSArray *topSource;
@property (nonatomic , strong)NSArray *bottomSource;

@end

@implementation KewaikuozhanCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.topSource = @[@{@"title":@"背单词",@"im":@"1"},@{@"title":@"单词PK",@"im":@"2"},@{@"title":@"刷单词",@"im":@"3"},@{@"title":@"错题本",@"im":@"4"},@{@"title":@"音标专练",@"im":@"6"}];//@{@"title":@"看一看",@"im":@"5"},

        self.bottomSource = @[@{@"title":@"听说",@"im":@"tingshuo"},@{@"title":@"听力",@"im":@"tingli"},@{@"title":@"跟读",@"im":@"gendu"},@{@"title":@"笔试",@"im":@"bishi"},@{@"title":@"音标",@"im":@"yinbiao"},@{@"title":@"典范",@"im":@"dianfan"},@{@"title":@"新概念",@"im":@"xingainian"},@{@"title":@"黑布林",@"im":@"heibulin"}];//,@{@"title":@"趣味",@"im":@"quwei"}


        [self setupUI];
        [self setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    }
    
    return self;
    
}

- (void)setupUI {
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = COLOR_WHITE;
    bgView.layer.cornerRadius = 15;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.top.bottom.equalTo(self.contentView).offset(0);
    }];

    [bgView addSubview:self.topViews];
    
    self.leftLine = [[UIView alloc] init];
    self.leftLine.backgroundColor = rgbHex(0xe5e5e5, 1);
    [bgView addSubview:self.leftLine];
    
    self.titleLB = [[UILabel alloc] init];
    self.titleLB.font = [UIFont boldSystemFontOfSize:14];
    self.titleLB.text = @"课外拓展";
    self.titleLB.textColor = rgbHex(0x333333, 1);
    [bgView addSubview:self.titleLB];
    
    self.rightLine = [[UIView alloc] init];
    self.rightLine.backgroundColor = rgbHex(0xe5e5e5, 1);
    [bgView addSubview:self.rightLine];

    [bgView addSubview:self.bottomViews];

    [self.topViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(bgView).offset(10);
        make.right.equalTo(bgView).offset(-10);
        make.height.mas_equalTo(180);
    }];
    [self.titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topViews.mas_bottom).offset(0);
        make.height.mas_equalTo(40);
        make.centerX.mas_equalTo(bgView.mas_centerX);
    }];
    [self.leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.titleLB.mas_left).offset(-15);
        make.left.equalTo(bgView).offset(30);
        make.centerY.mas_equalTo(self.titleLB.mas_centerY);
        make.height.mas_equalTo(1);
    }];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLB.mas_right).offset(15);
        make.right.equalTo(bgView).offset(-30);
        make.centerY.mas_equalTo(self.titleLB.mas_centerY);
        make.height.mas_equalTo(1);
    }];
    [self.bottomViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(10);
        make.bottom.right.equalTo(bgView).offset(-10);
        make.height.mas_equalTo(150);
    }];

}

//懒加载创建collection view
- (UICollectionView *)topViews {

    if (!_topViews) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = (SCREEN_W - 40 - 80*3-60)/2;//x
        layout.minimumLineSpacing = 10; //y
        
        _topViews = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_W-40, 180) collectionViewLayout:layout];
        _topViews.delegate = self;
        _topViews.dataSource = self;
        _topViews.showsVerticalScrollIndicator = NO;
        _topViews.showsHorizontalScrollIndicator = NO;
        _topViews.alwaysBounceVertical = YES;
        _topViews.backgroundColor = COLOR_WHITE;

        //注册cell和header
        [_topViews registerClass:[ButtonCollectionCell class] forCellWithReuseIdentifier:@"top"];
        
    }
    
    return _topViews;
    
}

- (UICollectionView *)bottomViews {

    if (!_bottomViews) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = (SCREEN_W - 40 - 60*5-40)/4;//x
        layout.minimumLineSpacing = 15; //y
        
        _bottomViews = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _bottomViews.delegate = self;
        _bottomViews.dataSource = self;
        _bottomViews.showsVerticalScrollIndicator = NO;
        _bottomViews.showsHorizontalScrollIndicator = NO;
        _bottomViews.alwaysBounceVertical = YES;
        _bottomViews.backgroundColor = COLOR_WHITE;
        //注册cell和header
        [_bottomViews registerClass:[ButtonCollectionCell class] forCellWithReuseIdentifier:@"bottom"];
        
    }
    
    return _bottomViews;
    
}

#pragma mark ==== collection的代理

#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if(collectionView == self.topViews){
        return CGSizeMake(80, 80);
    }
    return CGSizeMake(60, 60);
    
}


#pragma mark ==== collection的数据源代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(collectionView == self.topViews){
        return self.topSource.count;
    }

    return self.bottomSource.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if(collectionView == self.topViews){
        ButtonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"top" forIndexPath:indexPath];
        NSDictionary *dic = self.topSource[indexPath.row];
        cell.titleLB.text = dic[@"title"];
        cell.imView.image = [UIImage imageNamed:dic[@"im"]];
        cell.titleLB.textColor = rgbHex(0x333333, 1);
        return cell;

    }
    
    ButtonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"bottom" forIndexPath:indexPath];
    NSDictionary *dic = self.bottomSource[indexPath.row];
    cell.titleLB.text = dic[@"title"];
    cell.imView.image = [UIImage imageNamed:dic[@"im"]];
    cell.titleLB.textColor = rgbHex(0x666666, 1);

    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if(collectionView == self.topViews){
        NSDictionary *dic = self.topSource[indexPath.row];
        self.menuItemBlock(dic[@"title"]);
    } else {
        if (!lApp.isVip) {
            UIViewController *vc = [[UIViewController alloc] initWithClassName:@"VipCenterBookDetailVC"];
            vc.paraObject = lApp.bookid;
            [(BaseNavigationController *)lApp.window.rootViewController pushViewController:vc animated:YES];
            return;
        }
        NSDictionary *dic = self.bottomSource[indexPath.row];
        self.menuItemBlock(dic[@"title"]);
    }
}



@end
